//
//  ViewController.m
//  自定义数字键盘
//
//  Created by TOPTEAM on 16/9/2.
//  Copyright © 2016年 TOPTEAM. All rights reserved.
//
#define RGB(__r, __g, __b)  [UIColor colorWithRed:(1.0*(__r)/255)\
green:(1.0*(__g)/255)\
blue:(1.0*(__b)/255)\
alpha:1.0]
#define SCREEN_HEIGHT [[UIScreen mainScreen] bounds].size.height
#define SCREEN_WIDTH [[UIScreen mainScreen] bounds].size.width
#define MARGIN   10
#define IF(A,B,C)  if (A) {B;}else{C;}
#define ONLYNum  @"0123456789."
#define Color_gray           RGB(136, 136, 136)//灰
#import "ViewController.h"
#import "WKKeyboard.h"
@interface ViewController ()<UITextFieldDelegate,WKDecimalKeyboardDelegate>
@property(nonatomic,strong)UITextField * textFeild_WK;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor cyanColor];
    _textFeild_WK=[[UITextField alloc]init];
    _textFeild_WK.tag=100;
    _textFeild_WK.backgroundColor=RGB(249, 250, 251);
    _textFeild_WK.clearButtonMode = UITextFieldViewModeNever;
    _textFeild_WK.returnKeyType = UIReturnKeyDone;
    _textFeild_WK.keyboardType = UIKeyboardTypeNumberPad;
    [_textFeild_WK setText:@"0"];
    [_textFeild_WK sizeToFit];
    [_textFeild_WK setFont:[UIFont systemFontOfSize:15.0f]];
    [_textFeild_WK setTextAlignment:NSTextAlignmentCenter];
    [_textFeild_WK setTextColor:RGB(242 ,38 ,19)];
    UIView *keyView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    keyView.backgroundColor = RGB(236, 236, 236);
    UIButton  * btn_cancle_money = [[UIButton alloc] initWithFrame:CGRectMake(MARGIN, MARGIN, 50, 20)];
    [btn_cancle_money setTitle:@"取消" forState:UIControlStateNormal];
    [btn_cancle_money addTarget:self action:@selector(action_cancle_money) forControlEvents:UIControlEventTouchUpInside];
    [btn_cancle_money setTitleColor:Color_gray forState:UIControlStateNormal];
    [keyView addSubview:btn_cancle_money];
    UIButton  * btn_finish_money = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 6 * MARGIN, MARGIN, 50, 20)];
    [btn_finish_money setTitle:@"完成" forState:UIControlStateNormal];
    [btn_finish_money addTarget:self action:@selector(action_finish_money) forControlEvents:UIControlEventTouchUpInside];
    [btn_finish_money setTitleColor:[ViewController fromHexValue:0x03bfa0] forState:UIControlStateNormal];
    [btn_finish_money setTintColor:[UIColor redColor]];
    [keyView addSubview:btn_finish_money];
    _textFeild_WK.inputAccessoryView = keyView;
    _textFeild_WK.delegate=self;
    _textFeild_WK.frame=CGRectMake( 100 , 100 , 200 , 50 );
    [self.view addSubview:_textFeild_WK];
    
    _textFeild_WK.delegate=self;
    WKKeyboard* inputView = [[WKKeyboard alloc] init];
    inputView.keyboard_Delegate=self;
    _textFeild_WK.inputView = inputView;
    [_textFeild_WK reloadInputViews];
    
}
#pragma mark-点击完成
- (void)action_finish_money{
    [_textFeild_WK resignFirstResponder];
}
#pragma mark-点击取消
- (void)action_cancle_money{
    [_textFeild_WK resignFirstResponder];
}
#pragma mark-输入小数点
- (void)KeyboardDelegate_clicked_Dot{

    NSLog(@"输入小数点");
}
#pragma mark-点击删除
- (void)KeyboardDelegate_clicked_Delete{
    NSLog(@"点击删除");
  
}
#pragma mark-点击收回键盘
- (void)KeyboardDelegate_clicked_Dismiss{
    NSLog(@"点击收回键盘");

}
#pragma mark-点击确定
- (void)KeyboardDelegate_clicked_Sure{
    NSLog(@"点击确定");
 
}
#pragma mark-输入字符
- (void)KeyboardDelegate_clicked_Decimal:(int)decimal{
    NSLog(@"输入字符");
   
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
+ (UIColor *)fromHexValue:(NSUInteger)hex {
    NSUInteger a = ((hex >> 24) & 0x000000FF);
    float fa = ((0 == a) ? 1.0f : (a * 1.0f) / 255.0f);
    
    return [ViewController fromHexValue:hex alpha:fa];
}
+ (UIColor *)fromHexValue:(NSUInteger)hex alpha:(CGFloat)alpha {
    if ( hex == 0xECE8E3 ) {
        
    }
    NSUInteger r = ((hex >> 16) & 0x000000FF);
    NSUInteger g = ((hex >> 8) & 0x000000FF);
    NSUInteger b = ((hex >> 0) & 0x000000FF);
    
    float fr = (r * 1.0f) / 255.0f;
    float fg = (g * 1.0f) / 255.0f;
    float fb = (b * 1.0f) / 255.0f;
    
    return [UIColor colorWithRed:fr green:fg blue:fb alpha:alpha];
}
#pragma mark-输入字符限制
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{//设置只允许输入数字和小数点
    NSCharacterSet *cs;
    cs = [[NSCharacterSet characterSetWithCharactersInString:ONLYNum] invertedSet];
    NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""]; //按cs分离出数组,数组按@""分离出字符串
    BOOL canChange = [string isEqualToString:filtered];
    return canChange;
}
@end


















